<html>
<head><meta charset="utf-8"><title>NonNull From&lt;Box&lt;T&gt;&gt; · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html">NonNull From&lt;Box&lt;T&gt;&gt;</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236494939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/236494939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#236494939">(Apr 28 2021 at 12:08)</a>:</h4>
<p>Is there a reason why <code>std::ptr::NonNull&lt;T:?Sized&gt;</code> doesn't implement <code>From&lt;Box&lt;T: ?Sized&gt;&gt;</code>? </p>
<p>I mean in it's design <code>NonNull</code> seems to mainly meant to handle smart pointer and similar  "owning pointers" (<code>LinkedList</code> Node, or <code>Arc</code>).</p>
<p>And having to go through <code>unsafe { NonNull::new_unchecked(Box::into_raw(data)) }</code> is introducing unsafe blocks in places where (at least theoretically)  they are not needed.</p>



<a name="236495288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/236495288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#236495288">(Apr 28 2021 at 12:11)</a>:</h4>
<p>Implementing <code>From&lt;Box&lt;T&gt;&gt; for NonNull&lt;T&gt;</code> would make it very easy to accidentally leak things by doing <code>function_accepting_non_null(my_box.into())</code>.</p>



<a name="236495759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/236495759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#236495759">(Apr 28 2021 at 12:15)</a>:</h4>
<p>That's a good point.</p>
<p>Through if there is no other we <em>maybe</em> want to add a <code>NonNull::from_box</code> method which mentions in the documentation why it's not a <code>From&lt;&gt;</code> implementation.  I guess I might write a micro RFC up this weekend <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="236499226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/236499226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#236499226">(Apr 28 2021 at 12:40)</a>:</h4>
<p><span class="user-mention" data-user-id="209646">@Philipp Korber</span> there was a proposal to add more <code>leak</code> functions, including one that gives you a <code>NonNull&lt;T&gt;</code>: <a href="https://github.com/KodrAus/rfcs/blob/container-leak/text/0000-container-leak.md">https://github.com/KodrAus/rfcs/blob/container-leak/text/0000-container-leak.md</a></p>



<a name="236499327"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/236499327" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#236499327">(Apr 28 2021 at 12:41)</a>:</h4>
<p>(but the author didn't have time to keep working on that: <a href="https://github.com/rust-lang/rfcs/pull/2969#issuecomment-802392063">https://github.com/rust-lang/rfcs/pull/2969#issuecomment-802392063</a>)</p>



<a name="236546273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/236546273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#236546273">(Apr 28 2021 at 17:25)</a>:</h4>
<blockquote>
<p>NonNull::from_box method which mentions in the documentation why it's not a From&lt;&gt; implementation. I guess I might write a micro RFC up this weekend <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>
</blockquote>
<p>Probably doesnt need an RFC if its just a libs function, it can start as unstable and then when stablilizing could FCP into reality. That said, I guess this would be the place for a temperature check on such a thing...</p>



<a name="236546363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/236546363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#236546363">(Apr 28 2021 at 17:25)</a>:</h4>
<p>it'd probably make more sense as part of Box rather than NonNull. see that RFC i linked.</p>



<a name="237120262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237120262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237120262">(May 03 2021 at 06:51)</a>:</h4>
<p>I have a vague recollection of trying to add <code>impl From&lt;Box&lt;T&gt;&gt; for NonNull&lt;T&gt;</code> and the compiler not accepting it. Maybe because of the liballoc vs libcore split, or because of <code>#[fundamental]</code>. (Or maybe I’m confused with some other <code>impl</code>)</p>



<a name="237637558"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237637558" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237637558">(May 06 2021 at 10:38)</a>:</h4>
<p><span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span> I think a bit of both. Due to the automatically generated <code>Into</code> based on the <code>From</code> and <code>Box</code> being <code>fundamental</code> this probably fails. And adding a <code>from_box</code> would fail because of the split (as long as there is no trick in std to implement methods on external (core) types).</p>



<a name="237639167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237639167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237639167">(May 06 2021 at 10:55)</a>:</h4>
<p>maybe as an associated function like <code>into_raw</code>?</p>



<a name="237650893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237650893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237650893">(May 06 2021 at 12:41)</a>:</h4>
<p>What's wrong with <code>NonNull::from(Box::leak(b))</code>?</p>



<a name="237819630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237819630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237819630">(May 07 2021 at 13:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E/near/236495288">said</a>:</p>
<blockquote>
<p>Implementing <code>From&lt;Box&lt;T&gt;&gt; for NonNull&lt;T&gt;</code> would make it very easy to accidentally leak things by doing <code>function_accepting_non_null(my_box.into())</code>.</p>
</blockquote>



<a name="237820425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237820425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237820425">(May 07 2021 at 13:16)</a>:</h4>
<p><span class="user-mention silent" data-user-id="330154">The 8472</span> <a href="#narrow/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E/near/237639167">said</a>:</p>
<blockquote>
<p>maybe as an associated function like <code>into_raw</code>?</p>
</blockquote>
<p>The problem is <code>Box::into_raw</code> already exists but returns a <code>*mut T</code> instead of an <code>NonNull&lt;T&gt;</code> mainly because it predates the existence of <code>NonNull</code>.</p>
<p>Same is true for <code>Arc</code>/<code>Rc</code>/<code>Weak</code> <code>into_raw</code> (but they are a bit special anyway), <code>CString::into_raw</code>.</p>
<p><code>Vec</code> and <code>String</code>'s <code>into_raw_parts</code> might still be changed to use <code>NonNull</code> as they are not stable.  On github it seems people are generally in favor of this, but it does introduce inconsistencies with the "older' <code>into_raw</code> methods.</p>



<a name="237820515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237820515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237820515">(May 07 2021 at 13:16)</a>:</h4>
<p>Sadly we (currently) can't magically change the old <code>into_raw</code> methods to return <code>NonNull</code> .</p>



<a name="237820678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237820678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237820678">(May 07 2021 at 13:18)</a>:</h4>
<p>So I thought about maybe proposing something like <code>into_raw</code> but using a alternative name.</p>



<a name="237834653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237834653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237834653">(May 07 2021 at 14:47)</a>:</h4>
<p>My "like" referred to <code>into_raw</code>'s nature as associated function, not as a name proposal ;)</p>



<a name="237844746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237844746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237844746">(May 07 2021 at 15:47)</a>:</h4>
<p>into_nonnull would be fine</p>



<a name="237857279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237857279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237857279">(May 07 2021 at 17:07)</a>:</h4>
<p>For box and similar yes, without question. But what about <code>Vec</code>? Where it returns <code>(NonNull&lt;T&gt;, usize, usize)</code>?</p>
<p>Just to be clear I'm not saying it doesn't work there, just that I'm not sure about it. </p>
<p>I would prefer a naming schema we can use for all types having a "into raw" transformation returning a <code>NonNull</code> as it's main component.</p>



<a name="237864209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237864209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237864209">(May 07 2021 at 17:52)</a>:</h4>
<p>Is there any benefit from NonNull pointers for? They may be dangling anyway for 0 capacity. Well, I guess the tuple would have a niche...</p>



<a name="237867704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237867704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237867704">(May 07 2021 at 18:16)</a>:</h4>
<p>In the return of raw parts, there's no advantage.  But it saves people doing <code>/* SAFETY: grr, this is never null but the method returns the wrong type */ NonNull::new_unchecked(the_ptr)</code> when they want to pass it to something else that doesn't want to worry about nulls.</p>



<a name="237869661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/237869661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#237869661">(May 07 2021 at 18:31)</a>:</h4>
<p>bit of a drag to have concurrent raw and nonnull methods all over</p>



<a name="240175114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/240175114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#240175114">(May 25 2021 at 11:49)</a>:</h4>
<p>FWIW currently the compiler can optimize out the null checks for all the standard library types <em>except</em> for <code>Cstring</code>(which for some reason doesn't inline <code>CString::into_inner</code>)<br>
<a href="https://godbolt.org/z/YPKW9G5xn">https://godbolt.org/z/YPKW9G5xn</a></p>



<a name="240178263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/240178263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#240178263">(May 25 2021 at 12:22)</a>:</h4>
<p>I’d be hesitant to rely on that. And even optimizations aside, <code>unwrap</code> / <code>expect</code> can be a code smell so I’d prefer having a comment "this never panics because blah blah"</p>



<a name="240308573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/NonNull%20From%3CBox%3CT%3E%3E/near/240308573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/NonNull.20From.3CBox.3CT.3E.3E.html#240308573">(May 26 2021 at 11:18)</a>:</h4>
<p>In any case, I've opened <a href="https://github.com/rust-lang/rust/pull/85719">https://github.com/rust-lang/rust/pull/85719</a> to fix the fact that the NonNull check for CString isn't optimized out</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>